package com.tudouji.project.d3.service.impl;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.MD5Utils;
import com.tudouji.common.utils.http.HttpUtils;
import com.tudouji.framework.config.KDNConfig;
import com.tudouji.project.d3.domain.LogisticSendfromRule;
import com.tudouji.project.d3.mapper.LogisticSendfromRuleMapper;
import org.apache.axis.encoding.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.d3.mapper.LogisticRuleMapper;
import com.tudouji.project.d3.domain.LogisticRule;
import com.tudouji.project.d3.service.ILogisticRuleService;

import static com.tudouji.common.utils.http.HttpUtils.map2Url;

/**
 * 发货地址配置Service业务层处理
 * 
 * @author tudouji
 * @date 2023-10-20
 */
@Service
public class LogisticRuleServiceImpl implements ILogisticRuleService 
{
    @Autowired
    private LogisticRuleMapper logisticRuleMapper;
    @Autowired
    private LogisticSendfromRuleMapper logisticSendfromRuleMapper;

    /**
     * 查询发货地址配置
     * 
     * @param id 发货地址配置ID
     * @return 发货地址配置
     */
    @Override
    public LogisticRule selectLogisticRuleById(Long id)
    {
        return logisticRuleMapper.selectLogisticRuleById(id);
    }

    /**
     * 查询发货地址配置列表
     * 
     * @param logisticRule 发货地址配置
     * @return 发货地址配置
     */
    @Override
    public List<LogisticRule> selectLogisticRuleList(LogisticRule logisticRule)
    {
        return logisticRuleMapper.selectLogisticRuleList(logisticRule);
    }

    /**
     * 新增发货地址配置
     * 
     * @param logisticRule 发货地址配置
     * @return 结果
     */
    @Override
    public int insertLogisticRule(LogisticRule logisticRule)
    {
        logisticRule.setCreateTime(DateUtils.getNowDate());
        return logisticRuleMapper.insertLogisticRule(logisticRule);
    }

    /**
     * 修改发货地址配置
     * 
     * @param logisticRule 发货地址配置
     * @return 结果
     */
    @Override
    public int updateLogisticRule(LogisticRule logisticRule)
    {
        logisticRule.setUpdateTime(DateUtils.getNowDate());
        return logisticRuleMapper.updateLogisticRule(logisticRule);
    }

    /**
     * 批量删除发货地址配置
     * 
     * @param ids 需要删除的发货地址配置ID
     * @return 结果
     */
    @Override
    public int deleteLogisticRuleByIds(Long[] ids)
    {
        return logisticRuleMapper.deleteLogisticRuleByIds(ids);
    }

    /**
     * 删除发货地址配置信息
     * 
     * @param id 发货地址配置ID
     * @return 结果
     */
    @Override
    public int deleteLogisticRuleById(Long id)
    {
        return logisticRuleMapper.deleteLogisticRuleById(id);
    }

    @Override
    public String getSendFromByRule(String logisticsOrderNo) throws Exception{
       /* LogisticRule logisticRule = logisticRuleMapper.getSendFromByRule(logisticsOrderNo);
        if (null == logisticRule){
            return "";
        }
        return logisticRule.getSendfrom();*/
        String URL = KDNConfig.getUrl();
        String ApiKey = KDNConfig.getApiKey();
        HashMap<String, String> requestDataMap = new HashMap<>();
        requestDataMap.put("LogisticCode", logisticsOrderNo);
        String requestData = JSONObject.toJSONString(requestDataMap);

        String base64Str = Base64.encode(MD5Utils.encryStringToLower(requestData + ApiKey).getBytes("UTF-8"));
        String DataSign = URLEncoder.encode(base64Str, "UTF-8");
        HashMap<String, String> params = new HashMap<>();

        params.put("RequestData", requestData);
        params.put("EBusinessID", KDNConfig.getEBusinessID());
        params.put("DataSign", DataSign);
        params.put("DataType", "2");
        params.put("RequestType", KDNConfig.getRequestType());
        String result = HttpUtils.sendPost(URL,map2Url(params),"application/x-www-form-urlencoded;charset=UTF-8","POST");


//        System.out.println("result: "+result);
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray traces = jsonObject.getJSONArray("Traces");
        if (traces.size() == 0){
            return null;
        }
        JSONObject item = traces.getJSONObject(0);
        String Location = item.getString("Location");
        Location = Location.trim();
        System.out.println("Location: " + Location);

        LogisticSendfromRule logisticSendfromRule = logisticSendfromRuleMapper.selectLogisticSendfromRuleByOrginalSendfrom(Location);
        if (null != logisticSendfromRule){
            System.out.println("orginal_sendFrom: " + Location + "final_sendFrom: " + logisticSendfromRule.getFinalSendfrom());
            return logisticSendfromRule.getFinalSendfrom();
        }
        return Location;
    }
}
